//
//  HomeVC.m
//  Project2
//
//  Created by hundredsandc on 2017/11/4.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "HomeVC.h"
#import "HomeCycyleView.h"
#import "HomeBtnCell.h"
#import "HomeCell.h"
#import "TouTiaoVC.h"


@interface HomeVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,ZXRollViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSMutableArray *gameArr;
@property (nonatomic, strong) ZXRollView *rollView;

@end

@implementation HomeVC
{
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    [self setupUI];
    [self getGameData];
    [self getData];
}
-(void)setupUI{
    [self.view addSubview:self.tableView];
    
    
}

-(void)getGameData{
    NSString *timeSp = [self getNowTimeTimestamp3];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"leagueId"]=@"4";
    params[@"page"] = @(0);
    params[@"pageSize"] = @"20";
    params[@"version"]= @"204";
    params[@"time"] = timeSp;
    params[@"sign"]=@"500ff05f4d877fbf9a14689ceac4c5eb";
    params[@"channel"]=@"AppStore";
    params[@"os_ver"] = @"10.2.1";
    params[@"platform"] = @"ios";
    [SVProgressHUD showWithStatus:@"请求中..."];
    [HTTPManagers getWithLiveURL:@"game_result_list" params:params success:^(id json) {
        
        NSArray *arr= [json objectForKey:@"data"];
        if (self.gameArr.count>0) {
            [_gameArr removeAllObjects];
        }
        for (int i = 0; i<3; i++) {
            NSDictionary *dict = arr[i];
            [self.gameArr addObject:dict];
        }
        [SVProgressHUD dismiss];
        [self addTableHeaderView];
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)addTableHeaderView{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 290)];
    SDCycleScrollView *headerCycleView =[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160) delegate:self placeholderImage:nil];
    headerCycleView.localizationImageNamesGroup = @[@"home_banner1",@"home_banner2",@"home_banner3"];
    headerCycleView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    headerCycleView.pageDotColor = [UIColor lightGrayColor];
    headerCycleView.autoScrollTimeInterval = 2.0;
    [headerView addSubview:headerCycleView];
    
    self.rollView = [[ZXRollView alloc] initWithFrame:CGRectMake(0,160,SCREEN_WIDTH,130)];
    self.rollView.pageIndicatorColor = [UIColor lightGrayColor];
    self.rollView.currentPageIndicatorColor = [UIColor redColor];
    self.rollView.delegate = self;
    self.rollView.autoRolling = YES;
    self.rollView.hideIndicatorForSinglePage = YES;
    self.rollView.interitemSpacing = 0;
    [self.rollView reloadViews];
    
    [headerView addSubview:self.rollView];
    
  
    _tableView.tableHeaderView = headerView;
}
#pragma mark <ZXRollViewDelegate>
- (NSInteger)numberOfItemsInRollView:(ZXRollView *)rollView {
    return 3;
}

- (void)rollView:(nonnull ZXRollView *)rollView setViewForRollView:(nonnull UIView *)view atIndex:(NSInteger)index {
   
            HomeCycyleView *cycyle = [[HomeCycyleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 130)];
            if (self.gameArr.count>0) {
                NSDictionary *dict = _gameArr[index];
            [cycyle customWithTiime:dict[@"timeString"] title:dict[@"leagueName"] subTime:nil TeamOneName:dict[@"homeTeamName"] TeamOneScore:dict[@"homeScore"] TeamTwoName:dict[@"awayTeamName"] TeamTwoScore:dict[@"awayScore"] subOne:@"3.23 2.23 0.02" subTwo:@"4.23 3.21 1.11"];
            }
            [view addSubview:cycyle];

   
    
}

- (void)rollView:(ZXRollView *)rollView didTapItemAtIndex:(NSInteger)index {
    if (rollView == _rollView) {
        NSLog(@"=== Tap the rollViewImage index %ld", index);
    } else {
        NSLog(@"*** Tap the rollViewColor index %ld", index);
    }
}

-(void)getData{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"channel_id"]=@(1);//头条
    params[@"access_token"] = @"";
    params[@"page_index"] = @(page);
    params[@"page_size"]= @"10";
    params[@"note_type"] = @"10";
    [SVProgressHUD showWithStatus:@"请求中..."];
    [HTTPManagers postNBAFootBallWithURL:@"new_notes_info_query" params:params success:^(id json) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        NSDictionary *dict = [json objectForKey:@"new_notes_info_query"];
        NSArray *arr = dict[@"data"];
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            [self.dataArr addObject:dict];
        }
        [SVProgressHUD dismiss];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        
        return 1;
    }else{
        return self.dataArr.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;
    }else{
        return 45;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45)];
    contentView.backgroundColor = [UIColor whiteColor];
    
    UILabel *title = [UIManage createUILabelFrame:CGRectMake(15, 20, 100, 15) withBackColor:nil withTextColor:RGBCOLOR(0x33, 0x33, 0x33) withTextFont:FONT(15) withTextAlignment:NSTextAlignmentLeft withTextContent:@"精选咨询"];
    [contentView addSubview:title];
    
    
    UIImageView *arrow = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-15-6, 33/2.0, 6, 12)];
    arrow.image = [UIImage imageNamed:@"home_more"];
    [contentView addSubview:arrow];
    
    UILabel *more = [UIManage createUILabelFrame:CGRectMake( arrow.left-8-50,15, 50, 15) withBackColor:nil withTextColor:RGBCOLOR(0x66, 0x66, 0x66) withTextFont:FONT(12) withTextAlignment:NSTextAlignmentRight withTextContent:@"更多"];
    [contentView addSubview:more];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerTap)];
    [contentView addGestureRecognizer:tap];
    return contentView;
}
-(void)headerTap{
    NSLog(@"tap");
    TouTiaoVC *vc = [TouTiaoVC new];
    
    vc.isMore = YES;
    [self.navigationController pushViewController:vc animated:YES];

    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.dataArr.count>0) {
        
        NSDictionary *dict = self.dataArr[indexPath.row];
        WEBViewController *web = [WEBViewController new];
        web.url = dict[@"share_url"];
        [self.navigationController pushViewController:web animated:YES];
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 125;
    }
    return 103;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
    HomeBtnCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeBtnCell class]) forIndexPath:indexPath];
        
        return cell;
    }else{
        HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeCell class]) forIndexPath:indexPath];
        if (self.dataArr.count>0) {
            
            NSDictionary *dict = self.dataArr[indexPath.row];
            
            [cell customTitle:dict[@"note_title"] time:dict[@"crt_time"] source:dict[@"channel_name"] imageUrlStr:dict[@"share_pic"]];
        }
        return cell;
    }
    
}
-(UITableView *)tableView{
    if (!_tableView) {
                _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-FTab) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeBtnCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeBtnCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeCell class])];
        
        _tableView.tableFooterView = [UIView new];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            page=1;
            [self getData];
           
        }];
   
    }
    return _tableView;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
-(NSMutableArray *)gameArr{
    if (!_gameArr) {
        _gameArr = [NSMutableArray array];
    }
    return _gameArr;
}
@end
